﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using HtmlAgilityPack;
using NewsCather.Models;
using ScrapySharp.Extensions;

namespace NewsCather.Service
{
    public class NewsCatherManager
    {
        public List<NewsList> GetNewsList(ParamsRecevier recevier)
        {
            List<NewsList> newsList = new List<NewsList>();
            HtmlWeb web = new HtmlWeb();
            web.OverrideEncoding = Encoding.Default;
            HtmlDocument homeDocument = web.Load(recevier.ListUrl);
            //获取List列表Content
            var listContainer = homeDocument.DocumentNode.CssSelect(recevier.ListContentSelector).FirstOrDefault();
            var aLis = listContainer.CssSelect("li a");
            //遍历获取里面的值
            NewsList newsListItem;
            foreach (var list in aLis)
            {
                newsListItem = new NewsList();
                newsListItem.Url = list.GetAttributeValue("href");
                newsListItem.Title = list.InnerText;
                newsList.Add(newsListItem);
            }
            return newsList;

            foreach (
                HtmlNode node in
                    homeDocument.DocumentNode.CssSelect("#allnews_all .items_area")
                        .FirstOrDefault()
                        .CssSelect(".hd .title")
                        .FirstOrDefault()
                        .CssSelect("a"))
                //驱动之家首页标题超链接
            {
                var url = node.Attributes["href"].Value;
                url = "http://www.cnbeta.com" + url;

                HtmlDocument articleDocument = web.Load(url);
                //移除不需要的部分
                foreach (var script in articleDocument.DocumentNode.Descendants("script").ToArray())
                    script.Remove();
                foreach (var style in articleDocument.DocumentNode.Descendants("style").ToArray())
                    style.Remove();
                foreach (var header in articleDocument.DocumentNode.Descendants("header").ToArray())
                    header.Remove();
                foreach (var footer in articleDocument.DocumentNode.Descendants("footer").ToArray())
                    footer.Remove();
                foreach (var top in articleDocument.DocumentNode.CssSelect(".goToTop").ToArray())
                    top.Remove();
                foreach (var fx in articleDocument.DocumentNode.CssSelect(".global_head").ToArray())
                    fx.Remove();
                foreach (var advance in articleDocument.DocumentNode.CssSelect("#BAIDU_DUP_wrapper_693405_0").ToArray())
                    advance.Remove();
                foreach (var img in articleDocument.DocumentNode.CssSelect("img").ToArray())
                {
                    if (img.ParentNode.Name == "a")
                    {
                        img.ParentNode.ParentNode.ReplaceChild(img, img.ParentNode);
                    }
                }
                //获取文章的标题
                var title = articleDocument.DocumentNode.CssSelect("#news_title").FirstOrDefault().InnerText;

                string content = string.Empty;

                var singlePageArticle = articleDocument.DocumentNode.CssSelect("div .article_content").FirstOrDefault();

            }
        }

        public List<News> GetNewsListContent(ParamsRecevier recevier, List<NewsList> newsList)
        {
            List<News> news = new List<News>();
            foreach (var list in newsList)
            {
                var newsEntity = GetNewsContent(recevier, list.Url);
                news.Add(newsEntity);
            }
            return news;
        }

        public News GetNewsContent(ParamsRecevier recevier, string newsUrl)
        {
            News news = new News();
            return news;
        }
    }
}